package com.zhongyu.user.provider.toolKit;

import com.zhongyu.comm.pojo.User;
import com.zhongyu.comm.util.Constants;
import com.zhongyu.comm.util.DateUtis;
import com.zhongyu.comm.vo.TokenVo;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Date;
import java.util.Map;

@Component
@ConfigurationProperties("jwt.config")
@Data
public class JwtTool {

    private String key;
    private int ttl;

    public TokenVo createJwt(User user){
        TokenVo vo=new TokenVo();
         LocalDateTime startTime=LocalDateTime.now();
         LocalDateTime endTime=startTime.plusMinutes(ttl);
        JwtBuilder jwtBuilder= Jwts.builder()
                .setId(user.getId()+"")
                .setSubject(user.getNickName())
                .setIssuedAt(DateUtis.asDate(startTime))
                .setExpiration(DateUtis.asDate(endTime))
                .signWith(SignatureAlgorithm.HS256,key)
                .claim(Constants.JWT_CLAIM_USER, user);
        String token=  jwtBuilder.compact();
        vo.setToken(token);
        vo.setExtTime(ttl*60);
        vo.setGenTime(startTime.toEpochSecond(ZoneOffset.of("+8")));

        return vo;
    }

    public Claims getClaims(String token){
        Claims claims=Jwts.parser()
                .setSigningKey(key)
                .parseClaimsJws(token).getBody();
        return claims;
    }

    public User parseJwt(String token){
        Claims claims=null;
         try {
             claims= Jwts.parser()
                     .setSigningKey(key)
                     .parseClaimsJws(token).getBody();
         }catch (Exception ex){
             return null;
         }
         if (claims!=null){

        Map<String,Object> userMap= (Map<String, Object>) claims.get("currentUser");
        userMap.forEach((k,v)->{
            if (v instanceof Map){
                Map value= (Map) v;
                Integer year=Integer.valueOf(value.get("year").toString());
                Integer month=Integer.valueOf(value.get("monthValue").toString());
                Integer day=Integer.valueOf(value.get("dayOfMonth").toString());
                Integer hour=Integer.valueOf(value.get("hour").toString());
                Integer minute=Integer.valueOf(value.get("minute").toString());
                Integer second=Integer.valueOf(value.get("second").toString());
                LocalDateTime localDateTime= LocalDateTime.of(year,month,day,hour,minute,second);
                userMap.put(k,localDateTime);
            }
        });
        User currentUser= new User();
        BeanMap.create(currentUser).putAll(userMap);
        return currentUser;
    }else {
             return null;
         }
    }

}
